﻿using MengXunAppTool.DAL;
using MengXunAppTool.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;


namespace 软件发布平台.View
{
    /// <summary>
    /// AppRunRecordView.xaml 的交互逻辑
    /// </summary>
    public partial class AppRunRecordView : UserControl
    {
        public AppRunRecordView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
            try
            {
                List<string> item = verDAL.GetCustomers();
                if (item.Count>0)
                {
                    CbxCus.ItemsSource = item;
                    CbxCus.Text = verDAL.GetCustomers()[0];
                    CbxCus_DropDownClosed(null, null);
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }

        private VerDAL verDAL = new VerDAL();
        private RunDAL runDAL = new RunDAL();
        private List<ViewModel.AppRunRecordViewModel.AppItem> appitems = new List<ViewModel.AppRunRecordViewModel.AppItem>();
        private List<Sql3Helper.MX_AppRunRecord> run_Apps = new List<Sql3Helper.MX_AppRunRecord>();
        private ViewModel.AppRunRecordViewModel viewModel = new ViewModel.AppRunRecordViewModel();
        public SolidColorBrush FirstColor = new SolidColorBrush(Color.FromRgb(152, 214, 230));
        public SolidColorBrush SecondColor = new SolidColorBrush(Color.FromRgb(238, 238, 133));
        private void CbxCus_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                CbxCus.ItemsSource = verDAL.GetCustomers();
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }

        private void CbxCus_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "")
                {
                    run_Apps = runDAL.GetApps(CbxCus.Text).OrderBy(it => it.Customer).ThenBy(it => it.Product).ThenBy(it => it.AppName).ThenByDescending(it=>it.Version).ThenByDescending(it=>it.RunTime).ToList();
                    List<ViewModel.AppRunRecordViewModel.AppItem> items = new List<ViewModel.AppRunRecordViewModel.AppItem>();
                    SolidColorBrush brush = FirstColor;
                    if (run_Apps.Count != 0)
                    {
                        TimeSpan timeSpan = DateTime.Now - run_Apps[0].RunTime;
                        int days = timeSpan.Days;
                        byte R = (byte)(days > 90 ? 255 : days * 2.83);
                        for (int i = 0; i < run_Apps.Count; i++)
                        {
                            ViewModel.AppRunRecordViewModel.AppItem appItem = new ViewModel.AppRunRecordViewModel.AppItem()
                            {
                                Cus = run_Apps[i].Customer,
                                Pro = run_Apps[i].Product,
                                AppName = run_Apps[i].AppName,
                                Ver = run_Apps[i].Version,
                                User = run_Apps[i].User,
                                RunTime = run_Apps[i].RunTime.ToString("yyyy-MM-dd HH:mm:ss"),
                                IP = run_Apps[i].IP
                            };
                            if (i > 0 && 
                                (run_Apps[i - 1].Version != appItem.Ver
                                || run_Apps[i - 1].AppName != appItem.AppName 
                                || run_Apps[i - 1].Customer != appItem.Cus
                                || run_Apps[i - 1].Product != appItem.Pro))
                            {
                                timeSpan = DateTime.Now - run_Apps[i].RunTime;
                                days = timeSpan.Days;
                                R = (byte)(days > 90 ? 255 : days * 2.83);
                                if (brush == FirstColor)
                                {
                                    brush = SecondColor;
                                }
                                else
                                {
                                    brush = FirstColor;
                                }
                            }
                            appItem.RunMsg = days.ToString() + "天前使用过";
                            appItem.RunMsgColor = new SolidColorBrush(Color.FromRgb(R, 50, 50));
                            //if (i > 0 && run_Apps[i - 1].IP != appItem.IP)
                            //{
                            //    if (brush == FirstColor)
                            //    {
                            //        brush = SecondColor;
                            //    }
                            //    else
                            //    {
                            //        brush = FirstColor;
                            //    }
                            //}
                            appItem.BackColor = brush;
                            items.Add(appItem);
                        }
                    }
                    appitems = items;
                    viewModel.AppItems = items;
                    CbxPro.Text = "";
                    CbxName.Text = "";
                    CbxName.ItemsSource = new List<string>();
                    CbxVer.Text = "";
                    CbxVer.ItemsSource = new List<string>();
                }              
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }



        private void CbxPro_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "")
                {
                    CbxPro.ItemsSource = verDAL.GetProducts(CbxCus.Text);
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }

        private void CbxPro_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "" && CbxPro.Text != "")
                {
                    List<ViewModel.AppRunRecordViewModel.AppItem> items = appitems.Where(it => it.Cus == CbxCus.Text && it.Pro == CbxPro.Text).ToList();
                    viewModel.AppItems = items;
                    CbxName.Text = "";
                    CbxVer.Text = "";
                    CbxVer.ItemsSource = new List<string>();
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }



        private void CbxName_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                if (CbxPro.Text != "")
                {
                    CbxName.ItemsSource = verDAL.GetAppNames(CbxCus.Text, CbxPro.Text);
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }

        private void CbxName_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "" && CbxPro.Text != "" && CbxName.Text != "")
                {
                    List<ViewModel.AppRunRecordViewModel.AppItem> items = appitems.Where(it => it.Cus == CbxCus.Text && it.Pro == CbxPro.Text && it.AppName == CbxName.Text).ToList();
                    viewModel.AppItems = items;
                    CbxVer.Text = "";
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }



        private void CbxVer_DropDownOpened(object sender, EventArgs e)
        {
            try
            {
                if (CbxPro.Text != "")
                {
                    CbxVer.ItemsSource = verDAL.GetVer(CbxCus.Text, CbxPro.Text,CbxName.Text);
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }

        private void CbxVer_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (CbxCus.Text != "" && CbxPro.Text != "" && CbxName.Text != "")
                {
                    List<ViewModel.AppRunRecordViewModel.AppItem> items = appitems.Where(it => it.Cus == CbxCus.Text && it.Pro == CbxPro.Text && it.AppName == CbxName.Text&&it.Ver==CbxVer.Text).ToList();
                    viewModel.AppItems = items;
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
               LogHelper.WriteLog(ee.ToString());
            }
        }
    }
}
